package com.example.springboot_carlos.Controller;

import com.example.springboot_carlos.Util.DBUtil;
import com.example.springboot_carlos.entiy.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

import java.sql.SQLException;

@RestController
public class UserController {

    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String nickname=request.getParameter("nickname");
        String age =request.getParameter("age");
        String passwordAgain= request.getParameter("passwordAgain");
        User user = new User(username,password,nickname,age);
        try (Connection connection = DBUtil.getConnection();){
            if(useFull.isnull(user)){
                response.sendRedirect("/reg_info_error.html");
                return;
            }
            if(!password.equals(passwordAgain)){
                response.sendRedirect("/reg_password_fail.html");
                return;
            }
            if (!useFull.have_user(connection,user).equals("No User")){
                response.sendRedirect("/have_user_fail.html");
                return;
            }
            useFull.intoDataBase(connection,user);
            response.sendRedirect("/reg_success.html");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    @RequestMapping("/updatePassword")
    public void updatePassword(HttpServletRequest request,HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String new_password = request.getParameter("new_password");
        String new_password_again= request.getParameter("new_password_again");
        User user = new User(username,password,null,null);
        try ( Connection connection= DBUtil.getConnection();){
            if(username==null||username.isEmpty()||
                    password==null||password.isEmpty()||
                    new_password==null||new_password.isEmpty()||
                    new_password_again==null||new_password_again.isEmpty()){
                response.sendRedirect("/change_info_error.html");
            }
            String userdata= useFull.have_user(connection,user);
            if(userdata.equals("No User")){
                response.sendRedirect("/change_username_fail.html");
                return;
            }else if(!userdata.equals(password)){
                response.sendRedirect("change_password_error.html");
                return;
            }
            if(!new_password.equals(new_password_again)){
                response.sendRedirect("/change_again_password_fail.html");
            }else{
                useFull.updateDataBasePassword(connection,username,new_password);
                response.sendRedirect("/change_success.html");
            }
        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }
    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password= request.getParameter("password");
        if(username==null||username.isEmpty()||
                password==null||password.isEmpty()){
            response.sendRedirect("login_info_error.html");
            return;
        }
        User user = new User(username,password,null,null);
        try (Connection connection =DBUtil.getConnection();){
            String passwordDataBase = useFull.have_user(connection,user);
            if(passwordDataBase.equals("No User")){
                response.sendRedirect("/login_username_fail.html");
                return;
            }else if(!passwordDataBase.equals(user.getPassword())){
                response.sendRedirect("/login_password_fail.html");
                return;
            }
            response.sendRedirect("/login_success.html");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


}
